package com.example.base.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.base.R;
import com.example.base.view.SysViewLayout;
import com.example.base.bean.SystemThree;
import com.example.base.bean.SystemTwo;

import java.util.ArrayList;
import java.util.List;

public class FlowAdapter extends BaseAdapter {
  private final List<SystemTwo> dataList = new ArrayList<>();
  private final Context context;
  private final String TAG = "FlowAdapter";

  public FlowAdapter(Context context, List<SystemTwo> dataList) {
    this.dataList.addAll(dataList);
    this.context = context;
  }

  @Override
  public int getCount() {
    return dataList.size();
  }

  @Override
  public java.lang.Object getItem(int position) {
    return null;
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @SuppressLint("InflateParams")
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    MyViewHolder myViewHolder;
    if (convertView == null) {
      myViewHolder = new MyViewHolder();
      LayoutInflater inflater = LayoutInflater.from(context);
      convertView = inflater.inflate(R.layout.list_view_sys, null, true);
      myViewHolder.tv_title = convertView.findViewById(R.id.tv_sys_title);
      myViewHolder.flowLayout = convertView.findViewById(R.id.ViewLayout);
      convertView.setTag(myViewHolder);
    } else {
      myViewHolder = (MyViewHolder) convertView.getTag();
    }
    SystemTwo systemTwo = dataList.get(position);
    myViewHolder.tv_title.setText(systemTwo.getName());
    initFlow(systemTwo.getChildren(), myViewHolder.flowLayout);
    return convertView;
  }

  private void initFlow(List<SystemThree> children, SysViewLayout sysViewLayout) {
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(10, 5, 10, 5);
    if (sysViewLayout == null) {
      Log.d(TAG, "sysViewLayout is null");
      return;
    }
    sysViewLayout.removeAllViews();
    for (int i = 0; i < children.size(); i++) {
      TextView tv = new TextView(context);
      tv.setPadding(28, 10, 28, 10);
      tv.setText(children.get(i).getName());
      tv.setMaxEms(10);
      tv.setSingleLine();
      tv.setBackgroundResource(R.drawable.shape_textback);
      tv.setLayoutParams(layoutParams);
      sysViewLayout.addView(tv, layoutParams);
    }
  }

  public void setData(List<SystemTwo> data) {
    if (null == data) {
      Log.d(TAG, "data is null so return");
      return;
    }
    Log.d(TAG, "data ==" + data);
    dataList.clear();
    dataList.addAll(data);
  }


  static class MyViewHolder {
    TextView tv_title;
    SysViewLayout flowLayout;
  }
}
